/*
 * Rui Santos 
 * Complete Project Details http://randomnerdtutorials.com
 */

#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is connected to the Arduino digital pin 2
#define ONE_WIRE_BUS 2

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature sensor 
DallasTemperature sensors(&oneWire);

unsigned long previousMillis = 0;
const long interval = 5000;


void setup() {
  // Start up the library
  sensors.begin();

  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
    // Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus
    sensors.requestTemperatures(); 
    
    // Celsius temperature
    // Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
    Serial.print(sensors.getTempCByIndex(0));
 
    // Fahrenheit temperature
    //Serial.println(sensors.getTempFByIndex(0));
  }
}
